﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SO_DataFetcher.PubFunctions.DataLink;
using SO_DataFetcher.PubFunctions.Paraments;
using SO_DataFetcher.PubFunctions.SqlScripts;

namespace SO_DataFetcher.Forms
{
    public partial class FrmList : Form
    {
        private string _type;
        public string[] _keyStrings;

        public FrmList(string type,string[] keyStrings)
        {
            InitializeComponent();

            this._type = type;
            this._keyStrings = keyStrings;

        }

        private void AddTypeList()
        {
            switch (_type)
            {
                case "DataLink":
                    
                    foreach (string key in PubDataLinks.DataLinks.Keys)
                    {
                        bool isSelect = this._keyStrings.Contains(key);
                        this.checkedListBox1.Items.Add(key, isSelect);
                    }

                    break;
                case "Scripts":
                    foreach (string key in PubScripts.Scripts.Keys)
                    {
                        bool isSelect = this._keyStrings.Contains(key);
                        this.checkedListBox1.Items.Add(key, isSelect);
                    }
                    break;
                case "Parament":
                    foreach (string key in PubParament.Paraments.Keys)
                    {
                        bool isSelect = this._keyStrings.Contains(key);
                        this.checkedListBox1.Items.Add(key, isSelect);

                    }
                    break;
            }
        }

        private void FrmList_Load(object sender, EventArgs e)
        {
            this.checkedListBox1.Items.Clear();

            AddTypeList();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.checkedListBox1.CheckedItems.Count < 1)
            {
                MessageBox.Show(@"没有选择任何数据!");
                return;
            }


            List<string> listItems = new List<string>();

            switch (_type)
            {
                case "DataLink":

                    if (this.checkedListBox1.CheckedItems.Count > 1)
                    {
                        MessageBox.Show(@"数据连接类型不能多选!");
                        return;
                    }

                    listItems.Add(this.checkedListBox1.CheckedItems[0].ToString());

                    break;
                case "Scripts":
                case "Parament":
                    foreach (var checkedItem in this.checkedListBox1.CheckedItems)
                    {
                        listItems.Add(checkedItem.ToString());
                    }
                   
                    break;
            }

            _keyStrings = listItems.ToArray();

            this.DialogResult = DialogResult.OK;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

    }
}
